<template>
  <div>
    <div style="margin:10px 5px; ">
      <el-input style="width:200px;margin-right: 5px;" suffix-icon="el-icon-search" v-model="name" placeholder="请输入名称"></el-input>
      <el-button type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>
    <div style="margin:20px 5px; ">
      <el-button type="primary" @click="handleAdd()">新增&nbsp;<i class="el-icon-circle-plus-outline"></i></el-button>
      <el-button type="danger" @click="delbatch">批量删除&nbsp;<i class="el-icon-remove-outline"></i></el-button>
    </div>
    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'" row-key="id" default-expand-all @selection-change="handleSelectionChange" :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id" align="center">
      </el-table-column>
      <el-table-column prop="name" label="名称" align="center">
      </el-table-column>
      <el-table-column prop="path" label="路径" align="center">
      </el-table-column>
      <el-table-column prop="pagePath" label="页面路径" align="center">
      </el-table-column>
      <el-table-column prop="icon" label="图标" align="center">
        <template slot-scope="scope">
          <span style="font-size:16px;" :class="scope.row.icon"></span>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" align="center">
      </el-table-column>
      <el-table-column prop="sortNum" label="顺序" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleAdd(scope.row.id)" v-if="!scope.row.pid && !scope.row.path">新增子菜单 <i class="el-icon-plus"></i></el-button>
          <el-button @click="handleEdit(scope.row)" type="success" size="mini"><i class="el-icon-edit"></i> 编辑</el-button>
          <el-button @click="del(scope.row.id)" type="danger" size="mini"><i class="el-icon-delete"></i> 删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" center width="30%">
      <el-form :rules="rules" ref="form" :model="form" size="small">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="路径" :label-width="formLabelWidth" prop="path">
          <el-input v-model="form.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="页面路径" :label-width="formLabelWidth" prop="pagePath">
          <el-input v-model="form.pagePath" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" :label-width="formLabelWidth">
          <el-select clearable v-model="form.icon" placeholder="请选择" style="width: 100%">
            <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
              <i :class="item.value" /> {{ item.name }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="顺序" :label-width="formLabelWidth">
          <el-input v-model="form.sortNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth" prop="description">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clear('form')">取 消</el-button>
        <el-button type="primary" @click="save('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'User',
  data () {
    return {
      options: [],
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      form: {
        name: '',
        path: '',
        pagePath: '',
        icon: '',
        sortNum: '',
        description: ''

      },
      name: "",
      dialogFormVisible: false,
      formLabelWidth: "80px",
      title: "菜单信息",
      multipleSelection: [],
      rules: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
        path: [
          { required: true, message: "请输入路径", trigger: "blur" },
        ],
        pagePath: [
          { required: true, message: "请输入顺讯", trigger: "blur" },
        ],

        description: [
          { required: true, message: "请输入描述", trigger: "blur" },
        ],
      }
    };
  },
  created () {
    this.load();
  },
  methods: {
    //查询数据
    load () {
      this.request.get("/menu", {
        params: {
          name: this.name
        }
      }).then(res => {
        this.tableData = res.data
      })
      this.request.get("/menu/icons").then(res => {
        console.log(res);
        this.options = res.data
      })
    },
    //新增
    save (form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.request.post("/menu/save", this.form).then(res => {
            console.log(res);
            if (res.code = "200") {
              this.$message.success("保存成功")
              this.dialogFormVisible = false
              this.load()
            } else {
              this.$message.error("保存失败")
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //编辑
    handleEdit (row) {
      this.form = row;
      this.dialogFormVisible = true;
      this.title = "编辑菜单";
    },
    //删除
    del (id) {
      this.request.delete("/menu/" + id).then(res => {
        this.$confirm("确认删除嘛, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          if (res.code = "200") {
            this.$message.success("删除成功");
            this.load();
          }
          else {
            this.$message.error("删除失败");
          }
        }).catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
      });
    },

    //批量删除
    handleSelectionChange (val) {
      console.log(val);
      this.multipleSelection = val;
    },
    //点击批量删除的时候
    delbatch () {
      let ids = this.multipleSelection.map(r => r.id); // [{},{},{}]把对象数组变成纯ID数组 [1,2,3]
      if (ids.length == '') {
        return alert("请先选择需要删除的菜单")
      }
      this.request.post("/menu/del/batch", ids).then(res => {
        this.$confirm("你确定批量删除这些数据嘛, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          if (res.code = "200") {
            this.$message.success("批量删除成功");
            this.load();
          }
          else {
            this.$message.error("批量删除失败");
          }
        }).catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
      });
    },
    handleAdd (pid) {
      this.dialogFormVisible = true
      this.form = {}
      if (pid) {
        this.form.pid = pid
      }
    },
    handleSizeChange (val) {
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange (val) {
      this.pageNum = val;
      this.load();
    },
    //重置
    reset () {
      this.name = "";
      this.load();
    },
    //点击取消
    clear (form) {
      if (this.$refs[form] != undefined) {
        this.$refs[form].resetFields();
      }
      this.load()
      this.dialogFormVisible = false
    }
  },

};
</script>

<style lang="less" scoped>
</style>